/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.validation;

import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import telsoft.cms_admin.login.LoginBean;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class LoginFilter implements Filter {

    private String oldURL = "";
    public static final String OLD_PATH = "url";
    public static boolean info = false;
    public static int count = 0;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        String pageRequested = req.getRequestURI().toString();
        HttpSession session = req.getSession(false);
        Object username = session == null ? null : session.getAttribute(LoginBean.AUTH_KEY);
        String notloggedinURL = req.getContextPath() + "/tamnhat/login.xhtml";

        String homeURL = req.getContextPath() + "/tamnhat/home.xhtml";
//      user not logged in AND accessing page other than login.xhtml, REDIRECT to login.xhtml
        if (username == null && !pageRequested.endsWith("login.xhtml")) {
            oldURL = pageRequested;
            if ("partial/ajax".equals(req.getHeader("Faces-Request"))) {
                res.setContentType("text/xml");
                res.getWriter().append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                        .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", notloggedinURL);
            } else {
                res.sendRedirect(notloggedinURL);
            }
        } else if (username != null && pageRequested.endsWith("login.xhtml")) { //user logged in AND accessing login.xhtml, REDIRECT to home.xhtml
            if(!oldURL.equals("") && !oldURL.endsWith("login.xhtml") && !oldURL.endsWith("/")) req.getSession().setAttribute("oldURL", oldURL.replace(req.getContextPath(), ""));
            res.sendRedirect(homeURL);
        } else { // otherwise, just pass this filter
            if(!oldURL.equals("") && !oldURL.endsWith("login.xhtml") && !oldURL.endsWith("/")) req.getSession().setAttribute("oldURL", oldURL.replace(req.getContextPath(), ""));
            chain.doFilter(request, response);
        }
    }
    
    public String getOldURL() {
        return oldURL;
    }

    public void setOldURL(String oldURL) {
        this.oldURL = oldURL;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //
    }

    @Override
    public void destroy() {
        //
    }
}
